package de.dlrg.wms.gui.views;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import de.dlrg.wms.controller.controllers.AgeClassController;
import de.dlrg.wms.gui.FormContainer;
import de.dlrg.wms.gui.forms.AgeClassForm;
import de.dlrg.wms.gui.forms.generic.AbstractTableViewWithDeleteEdit;
import de.dlrg.wms.gui.views.coulmnNames.AgeClassColumnNames;
import de.dlrg.wms.model.AgeClass;

import static de.dlrg.wms.gui.views.coulmnNames.AgeClassColumnNames.*;

public class AgeClassTableView extends AbstractTableViewWithDeleteEdit {
	private AgeClassController ageClassController;

	public void refresh() {
		this.table.clearAll();
		this.table.setItemCount(0);

		List<AgeClass> ageClasses = this.ageClassController.readAll();
		for (AgeClass ageClass : ageClasses) {
			TableItem ageClassItem = new TableItem(this.table, SWT.NULL);

			ageClassItem.setData(ageClass);
			ageClassItem.setText(ID.ordinal(), "" + ageClass.getId());
			ageClassItem.setText(NAME.ordinal(), "" + ageClass.getName());
			ageClassItem.setText(DESCRIPTION.ordinal(), "" + ageClass.getCalculationFormula());

		}
	}

	public AgeClassTableView(Composite parent, int border,
			final AgeClassController ageClassController, final Shell shell) {
		super(parent, border);
		this.tableColumns = AgeClassColumnNames.class;
		this.ageClassController = ageClassController;
		this.initGUI(shell);
	}

	@Override
	protected void editPressed() {
		if (table.getSelection().length > 0 && table.getSelection().length < 2) {
			AgeClass selectedAgeClass = (AgeClass) table.getSelection()[0]
					.getData();
			FormContainer formContainer = new FormContainer();
			formContainer.createContents();
			AgeClassForm ageClassForm = new AgeClassForm(
					formContainer.getComposite(), SWT.BORDER,
					ageClassController, selectedAgeClass);
			ageClassForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
					true, 1, 1));
			ageClassForm.getClosing().addObserver(formContainer);
			formContainer.open();

			refresh();
		}
	}

	@Override
	protected void deletePressed() {
		if (table.getSelection().length > 0) {

			MessageBox messageBox = new MessageBox(shell, SWT.ICON_INFORMATION
					| SWT.OK | SWT.CANCEL);
			messageBox.setMessage("Do you want to delete "
					+ table.getSelectionCount() + " discipline(s)");
			int rc = messageBox.open();

			switch (rc) {
			case SWT.OK:
				break;
			default:
				return;
			}

			for (TableItem tableItem : table.getSelection()) {
				AgeClass toDelete = (AgeClass) tableItem.getData();
				ageClassController.delete(toDelete);
			}
			refresh();
		}
	}
}
